package com.yiwenup.leetcode.offer;

/**
 * https://leetcode-cn.com/problems/shu-zhi-de-zheng-shu-ci-fang-lcof/
 **/
public class No016 {

    /**
     * 执行用时：0 ms, 在所有 Java 提交中击败了100.00%的用户
     * 内存消耗：37.1 MB, 在所有 Java 提交中击败了66.90%的用户
     */
    public double myPow(double x, int n) {
        if (n == 0) return 1;
        if (x == 1 || x == 0) return x;

        double res = 1;
        long tmp = n;
        if (tmp < 0) {
            tmp = -tmp;
            x = 1 / x;
        }

        while (tmp > 0) {
            // 奇数
            if ((tmp & 1) == 1) res *= x;

            x *= x;
            tmp = tmp >> 1;
        }

        return res;
    }
}
